package cn.wolfcode.ssm.service.impl;

import cn.wolfcode.ssm.domain.Role;
import cn.wolfcode.ssm.mapper.RoleMapper;
import cn.wolfcode.ssm.page.PageResult;
import cn.wolfcode.ssm.query.QueryObject;
import cn.wolfcode.ssm.service.IRoleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class IRoleServiceImpl implements IRoleService {

    @Autowired
    private RoleMapper roleMapper;


    @Override
    public void save(Role role, Long[] ids) {

        roleMapper.insert(role);

        if (ids != null && ids.length > 0){
            for (Long pid : ids) {
                roleMapper.insertReslation(role.getId(),pid);
            }
        }
    }

    @Override
    public void update(Role role, Long[] ids) {

        roleMapper.updateByPrimaryKey(role);

        roleMapper.deleteReslation(role.getId());
        if (ids != null && ids.length > 0){
            for (Long pid : ids) {
                roleMapper.insertReslation(role.getId(),pid);
            }
        }


    }

    @Override
    public void delete(Long id) {
        roleMapper.deleteByPrimaryKey(id);
        roleMapper.deleteReslation(id);

    }

    @Override
    public Role get(Long id) {

        return roleMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Role> selectAll() {
        return roleMapper.selectAll();
    }

    @Override
    public PageInfo<Role> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<Role> PageResult = roleMapper.list(qo);
        return new PageInfo<Role>(PageResult);

    }
}
